$(function(){
	loadItemList();
	selectItemListBuyerGrade(false);
	selectBuyerGrades();
	//selectItemCatTree();
});

/**
 * 加载会员列表
 */
function loadItemList(){
	$('#buyerGradeList_dateGrid').datagrid({
		loadMsg : "数据加载中",
		title:"会员等级列表",
		border:true,
		fit : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : false,
		toolbar : "#buyerGradeList_dateGrid_toolbar",
		columns : [ [
		    {field:	 'checked',checkbox:true},
		    {field : 'id',hidden : true},
			{field : 'name',	title : '会员等级',	width : 80,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'isDefault',	title : '默认会员等级',		width : 100,align : 'center',hidden : false,
				formatter: function(value,row,index){
					if(value == 0){
						return "否";
					}else{
						return "是";
					}
					return 	"未知";
				}
			},
			{field : 'score',title : '积分',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return value;
				},
			},
			{field : 'discount',title : '折扣',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return value;
				},
			},
			{field : 'description',title : '备注',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return value;
				},
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
					var str = '<a href="#" style="color:blue;margin-left:3px;" onClick="showBuyerGrade('+row.id+')">查看</a>';
				    return str;
				}
			}
		] ],
		toolbar : [ {
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				addBuyerGrade();
			}
		},{
			text : '编辑',
			iconCls : 'icon-edit',
			handler : function() {
				showBuyerGrade();
			}
		}, {
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				deleteBuyerGrade();
			}		
		}, ]
	});
}

/**
 * 查询会员列表
 * @param haveparams 是否有参数，初试加载传入false，点击查询按钮传入true
 */
function selectItemListBuyerGrade(haveparams){
	var params = {};
	
	if(haveparams){
		var userName=$.trim($("#list_userName").val());          		//会员名
		var realName=$.trim($('#list_realName').val());                  //会员真是姓名
		var userLevel=$.trim($('#list_userLevel').combobox('getValue'));                 		//会员等级
		if(saleStatus == ''){
			saleStatus = [1,2,3];
		}else{
			saleStatus=[parseInt(saleStatus)];
		}
		params = {
			saleStatus : saleStatus,
			userName : userName,
			realName : realName,
			userLevel : userLevel,
		};
	}else{
		var saleStatus = [1,2,3];
		params = {
				saleStatus : saleStatus
			};
	}
	$("#buyerGradeList_dateGrid").datagrid_pagination($("#buyerGradeList_dateGrid"),path+"/buyer/v1/selectBuyerGrade",params,"","");
}

/**
 * 查询所有会员等级（用于下拉菜单）
 */
function selectBuyerGrades(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/buyer/v1/selectBuyerGrade?page=1&rows=10000',
		data:'{}',
		dataType:"json",
		success:function(data){
			$("#list_userLevel").combobox({
				valueField : 'id',
				textField : 'name',
				data : data.rows
			});
		}
   });
}

/**
 * 查看会员等级
 */
function showBuyerGrade(id){
	if(id == null || id == ''){
		id = getSelectionsIds();
		if(id.length == 0){
			$.messager.alert('提示','必须选择一个会员等级才能编辑!');
			return ;
		}
		if(id.length > 1){
			$.messager.alert('提示','只能选择一个会员等级!');
			return ;
		}
		id = id[0];
	}
	if(id != null && id != ''){
		var params = {id:id};
    	$.ajax({
    		type:"post",
    		//contentType:"application/json; charset=utf-8",
    		url:path+'/buyer/v1/showBuyerGrade',
    		data:params,
    		dataType:"json",
    		success:function(data){
    			$('#id').val(data.id);
				$('#name').val(data.name);
				$('#score').val(data.score);
    			$('#discount').val(data.discount);
    			$('#description').val(data.description);
    		}
       });
	}
	$('#itemBuyerGradeWindow').window("open");
}

/**
 * 保存会员等级
 * 
 */

function saveBuyerGrade(){
	var id = $.trim($("#id").val())
	var name = $.trim($("#name").val());
	var score = $.trim($("#score").val());
	var discount = $.trim($("#discount").val());
	var description = $.trim($("#description").val());
	
	if(name == null || name == ""){
		$.messager.alert("提示","会员等级名称不能为空");
		return;
	}
	if(score == null || score == ""){
		$.messager.alert("提示","积分满足点数不能为空");
		return;
	}
	if(discount == null || discount == ""){
		$.messager.alert("提示","会员等级价格");
		return;
	}
	
	var params={
			id : id,
			name : name,
			score : score,
			discount : discount,
			description : description
		};
	if(id != null && id != ""){		//修改
		$.ajax({
			type:"post",
			//contentType:"application/json; charset=utf-8",
			url:path+'/buyer/v1/updatebuyerGrade',
			data:params,
			dataType:"json",
			success:function(data){
				if(data.result == 1){
		    		$('#itemBuyerGradeWindow').window('close');
		    		selectItemListBuyerGrade(false);
		    	}else{
		    		$.messager.alert("提示",data.msg);
		    	}
			}
	   });
	}else{		//增加
		alert(name);
		$.ajax({
			type:"post",
			//contentType:"application/json; charset=utf-8",
			url:path+'/buyer/v1/addbuyerGrade',
			data:params,
			dataType:"json",
			success:function(data){
				if(data.result == 1){
		    		$('#itemBuyerGradeWindow').window('close');
		    		selectItemListBuyerGrade(false);
		    	}else{
		    		$.messager.alert("提示",data.msg);
		    	}
			}
	   });
		
	}
	
}

/**
 * 增加会员等级
 */
function addBuyerGrade(){
	$('#name').val('');
	$('#score').val('');
	$('#discount').val('');
	$('#description').val('');
	$('#itemBuyerGradeWindow').window("open");
}

/**
 * 删除会员
 */
function deleteBuyerGrade(id){
	if(id == '' || id == undefined){
		id = getSelectionsIds();
		if(id.length == 0){
			$.messager.alert('提示','未选中会员等级!');
			return ;
		}
	}
	$.messager.confirm('确认','确定删除此会员吗？',function(r){
	    if (r){
	    	var params = {};
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/buyer/v1/deletebuyerGrade/' + id,
	    		data:params,
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除会员等级成功!',undefined,function(){
	    					selectItemListBuyerGrade(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}


/**
 * 得到所选择行的id（int型）
 * @returns {Array}
 */
function getSelectionsIds(){
	var itemList = $("#buyerGradeList_dateGrid");
	var sels = itemList.datagrid("getSelections");
	var ids = new Array();
	for(var i in sels){
		ids.push(parseInt(sels[i].id));
	}
	return ids;
}



/**
 * 编辑商品，跳转到发布页面
 */
function editItem(){

	var ids = getSelectionsIds();
	if(ids.length == 0){
		$.messager.alert('提示','必须选择一个商品才能编辑!');
		return ;
	}
	if(ids.length > 1){
		$.messager.alert('提示','只能选择一个商品!');
		return ;
	}
	
	row = $("#buyerGradeList_dateGrid").datagrid("getSelected");
	
	$(".tree-title:contains('商品发布')").parent().click();
	
	var tab = $('#tabs').tabs('getSelected');  // 获取选择的面板
	tab.panel('refresh', 'auemall-item-add');

}

